<template>
  <div class="moldDoc-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain :type="'primary'" @click="query">
          查询
        </el-button>
        <el-button :icon="Plus" plain :type="'primary'" @click="toAdd">
          新增
        </el-button>
        <el-button :icon="Printer" plain type="primary" @click="toPrint">
          打印
        </el-button>
        <!-- <el-button
          :disabled="!state.currentRow"
          :icon="Edit"
          plain
          type="warning"
          @click="toUpdate()"
        >
          修改
        </el-button> -->
        <!-- <el-button
          :disabled="!state.currentRow"
          :icon="Finished"
          plain
          type="warning"
          @click="toUpdate()"
        >
          停用
        </el-button> -->
        <!-- <el-button
          :disabled="!state.currentRow"
          :icon="Delete"
          plain
          type="danger"
          @click="toDelete()"
        >
          删除
        </el-button> -->
        <!-- <el-button
          :disabled="!state.currentRow"
          :icon="Refresh"
          plain
          type="primary"
          @click="toChangePrem()"
        >
          权限修改
        </el-button> -->
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :data="moldDocs"
      highlight-current-row
      :row-style="{ fontSize: '8px' }"
      @current-change="handleCurrentChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="45" />
      <el-table-column
        label="模具编码"
        min-width="150"
        prop="moldCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="模具名称"
        min-width="150"
        prop="moldName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="模具型号"
        min-width="150"
        prop="moldSpec"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="单位"
        prop="measName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="状态" prop="state">
        <template #default="scope">
          <el-tag
            v-if="scope.row.state === 1"
            effect="dark"
            round
            type="primary"
          >
            启用
          </el-tag>
          <el-tag
            v-else-if="scope.row.state === 2"
            effect="dark"
            round
            type="warning"
          >
            停用
          </el-tag>
          <el-tag v-else effect="dark" round type="danger">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建人"
        prop="createUser"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        :show-overflow-tooltip="true"
      />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />

    <addMoldDocDialog ref="addMoldDocDialogRef" @success-callback="getData" />
    <queryDialog ref="queryDialogRef" @success-callback="queryCallback" />
  </div>
</template>

<script setup>
  import addMoldDocDialog from './modules/addMoldDocDialog.vue'
  import { getMoldDocPageApi } from '@/api/equip/mold_api'
  import { Plus, Search, Printer } from '@element-plus/icons-vue'
  import { localStorageConfig } from '@/config/localstorage.config'
  import queryDialog from './modules/queryDialog.vue'
  import hiPrint from '@/utils/hiPrint.js'

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const addMoldDocDialogRef = ref()
  const queryDialogRef = ref()
  const selectedRows = ref()

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const moldDocQuery = ref({
    moldCode: '',
    moldName: '',
    moldSpec: '',
    state: 1,
    pkCorp: pkCorp,
  })

  const moldDocs = ref()

  const query = () => queryDialogRef.value.openDialog()

  const queryCallback = (value) => {
    moldDocQuery.value = value
    getData()
  }

  const toAdd = () => {
    addMoldDocDialogRef.value.openDialog()
  }

  const handleCurrentChange = () => []

  const getData = async () => {
    const load = $baseLoading()
    try {
      let parms = {
        pageNum: paginationParams.pageNum,
        pageSize: paginationParams.pageSize,
        ...moldDocQuery.value,
      }
      const res = await getMoldDocPageApi(parms)
      if (res.code === 2000) {
        $baseMessage(res.msg, 'success')
        moldDocs.value = res.data.list
        paginationParams.total = res.data.total
      } else {
        $baseMessage(res.msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const handleSelectionChange = (rows) => {
    selectedRows.value = rows
  }

  const toPrint = () => {
    hiPrint('mold_001', selectedRows.value)
  }

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
    getData()
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
    getData()
  }
  //#endregion pagintion 结束

  onMounted(() => getData())
</script>

<style scoped></style>
